﻿using System;
using System.Collections.Generic;

namespace EasyComm.Caching
{
    public class CacheStateItemCollection : IDictionary<string, int>
    {
        #region private Dictionary<string, int> _innerDictionary
        /// <summary>
        /// Get/Sets the _innerDictionary of the <see cref="UI.WebCommon.Caching.CacheStateItemCollection"/>
        /// </summary>
        private Dictionary<string, int> _innerDictionary;
        #endregion

        private bool _isNew;
        #region public bool IsNew
        /// <summary>
        /// Gets the IsNew of the CacheStateItemCollection
        /// </summary>
        /// <value></value>
        public bool IsNew
        {
            get { return _isNew; }
        }
        #endregion

        #region public CacheStateItemCollection()
        /// <summary>
        /// Initializes a new instance of the <b>CacheStateItemCollection</b> class.
        /// </summary>
        public CacheStateItemCollection()
        {
            _innerDictionary = new Dictionary<string, int>();
            _isNew = true;
        }
        #endregion

        #region public void Add(string key, int value)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(string key, int value)
        {
            _innerDictionary.Add(key, value);
            _isNew = false;
        }
        #endregion

        #region public bool ContainsKey(string key)
        /// <summary>
        /// Returns a value indicating whether the specified <see cref="String"/>
        ///  is contained in the <see cref="UI.WebCommon.Caching.CacheStateItemCollection"/>.
        /// </summary>
        /// <param name="key">The <see cref="String"/> to locate in the 
        /// <see cref="UI.WebCommon.Caching.CacheStateItemCollection"/>.</param>
        /// <returns><b>true</b> if the <i>String</i> parameter is a member 
        /// of the <see cref="UI.WebCommon.Caching.CacheStateItemCollection"/>; otherwise, <b>false</b>.</returns>
        public bool ContainsKey(string key)
        {
            return _innerDictionary.ContainsKey(key);
        }
        #endregion

        #region public ICollection<string> Keys
        /// <summary>
        /// Gets the Keys of the CacheStateItemCollection
        /// </summary>
        /// <value></value>
        public ICollection<string> Keys
        {
            get { return _innerDictionary.Keys; }
        }
        #endregion

        #region public bool Remove(string key)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Remove(string key)
        {
            _isNew = false;
            return _innerDictionary.Remove(key);
        }
        #endregion

        #region public bool TryGetValue(string key, out int value)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryGetValue(string key, out int value)
        {
            return _innerDictionary.TryGetValue(key, out value);
        }
        #endregion

        #region public ICollection<int> Values
        /// <summary>
        /// Gets the Values of the CacheStateItemCollection
        /// </summary>
        /// <value></value>
        public ICollection<int> Values
        {
            get { return _innerDictionary.Values; }
        }
        #endregion

        #region public int this[string key]
        /// <summary>
        /// Get/Sets the <see cref="Int32"/> item identified by the given arguments of the CacheStateItemCollection
        /// </summary>
        /// <value></value>
        public int this[string key]
        {
            get
            {
                return _innerDictionary[key];
            }
            set
            {
                _isNew = false;
                _innerDictionary[key] = value;
            }
        }
        #endregion

        #region public void Add(KeyValuePair<string, int> item)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public void Add(KeyValuePair<string, int> item)
        {
            _isNew = false;
            _innerDictionary.Add(item.Key, item.Value);
        }
        #endregion

        #region public void Clear()
        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            _innerDictionary.Clear();
        }
        #endregion

        #region public bool Contains(KeyValuePair<string, int> item)
        /// <summary>
        /// Returns a value indicating whether the specified <see cref="KeyValuePair{String,Int32}"/>
        ///  is contained in the <see cref="UI.WebCommon.Caching.CacheStateItemCollection"/>.
        /// </summary>
        /// <param name="item">The <see cref="KeyValuePair{String,Int32}"/> to locate in the 
        /// <see cref="UI.WebCommon.Caching.CacheStateItemCollection"/>.</param>
        /// <returns><b>true</b> if the <i>KeyValuePair&lt;String,Int32&gt;</i> parameter is a member 
        /// of the <see cref="UI.WebCommon.Caching.CacheStateItemCollection"/>; otherwise, <b>false</b>.</returns>
        public bool Contains(KeyValuePair<string, int> item)
        {
            foreach (string key in _innerDictionary.Keys)
            {
                if (key == item.Key && _innerDictionary[key] == item.Value)
                {
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region public void CopyTo(KeyValuePair<string, int>[] array, int arrayIndex)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        public void CopyTo(KeyValuePair<string, int>[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region public int Count
        /// <summary>
        /// Gets the Count of the CacheStateItemCollection
        /// </summary>
        /// <value></value>
        public int Count
        {
            get { return _innerDictionary.Count; }
        }
        #endregion

        #region public bool IsReadOnly
        /// <summary>
        /// Gets the IsReadOnly of the CacheStateItemCollection
        /// </summary>
        /// <value></value>
        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }
        #endregion

        #region public bool Remove(KeyValuePair<string, int> item)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(KeyValuePair<string, int> item)
        {
            foreach (string key in _innerDictionary.Keys)
            {
                if (key == item.Key && _innerDictionary[key] == item.Value)
                {
                    _innerDictionary.Remove(key);
                    _isNew = false;
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region public IEnumerator<KeyValuePair<string, int>> GetEnumerator()
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<KeyValuePair<string, int>> GetEnumerator()
        {
            return _innerDictionary.GetEnumerator();
        }
        #endregion

        #region System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _innerDictionary.GetEnumerator();
        }
        #endregion

        #region public CacheStateItemCollection Clone()
        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>A new object that is a copy of this instance.</returns>
        public CacheStateItemCollection Clone()
        {
            CacheStateItemCollection col = new CacheStateItemCollection();
            foreach (KeyValuePair<string, int> item in _innerDictionary)
            {
                col.Add(item);
            }
            return col;
        }
        #endregion
    }
}
